package com.jzg.jcptadmin.presenter;

import com.jzg.jcptadmin.base.BasePresenter;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.SuccessData;
import com.jzg.jcptadmin.viewinterface.TaskDetailInterface;
import com.orhanobut.logger.Logger;

import java.util.Map;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by jzg on 2016/5/3.
 */
public class TaskDetailPresenter extends BasePresenter<TaskDetailInterface> {
    private DataManager mDataManager;
    private Subscription mSubscription;
    private TaskDetailInterface view;

    public TaskDetailPresenter(DataManager dataManager) {
        mDataManager = dataManager;

    }

    //@Override
    public void attachView(TaskDetailInterface mvpView) {
        super.attachView(mvpView);
        view = mvpView;
    }

    @Override
    public void detachView() {
        super.detachView();
        if (mSubscription != null) mSubscription.unsubscribe();
    }

    /**
     * 登陆
     */
    public void UpData() {
        Map<String, String> params = view.getParams();
        if (!checkSubmitInfo(params)) return;

        mSubscription = mDataManager.OperationEvaluationData(params).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<SuccessData>() {
                    @Override
                    public void onCompleted() {
                        view.dismissDialog();
                    }

                    @Override
                    public void onError(Throwable e) {

                        if (e != null && view != null) {
                            Logger.e("异常"+e.toString());
                            view.dismissDialog();
                            view.showError(e.toString());
                        }
                    }

                    @Override
                    public void onNext(SuccessData successData) {
                        view.showData(successData);
                    }
                });
    }

}
